package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.HttpStatusEnum;
import com.example.common.codeFactory.CodeGenerateStrategy;
import com.example.common.codeFactory.CodeGenerateStrategyFactory;
import com.example.common.codeFactory.entity.CodeGenerateType;
import com.example.entity.*;
import com.example.pojo.Menu;
import com.example.mapper.MenuMapper;
import com.example.pojo.Rule;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 菜单信息表(Menu)表服务实现类
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@Service
public class MenuService{
    // mapper对象
    @Resource
    private MenuMapper menuMapper;
    // 编号工厂
    @Resource
    private CodeGenerateStrategyFactory codeGenerateStrategyFactory;
    
    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param menu 筛选条件
     * @return
     */
    public ResponseData getByPage(Integer currentPage, Integer pageSize, Menu menu) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        //TODO: getByPage menu qw查询条件待构建
        IPage ipage = new Page(currentPage,pageSize);
        menuMapper.selectPage(ipage,qw);
        //若当前页码大于总页面数
        if (currentPage > ipage.getPages()){
            ipage = new Page(ipage.getPages(),pageSize);
            menuMapper.selectPage(ipage,qw);
        }
        if (ipage.getTotal()<=0){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(),ipage);
    }
    
    /**
     * 根据ID查询信息
     *
     * @param id 主键
     * @return 单条数据
     */
    public ResponseData findById(Long id) {
        Menu menu = menuMapper.selectById(id);
        if(menu == null || menu.getId() == null){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(),menu);
    }
    
    /**
     * 新增数据
     *
     * @param  menu 实体
     * @return 新增结果
     */
    public ResponseData add(Menu menu) {
        //插入
        if (menuMapper.insert(menu)>0){
            // 插入成功后要赋值编码
            CodeGenerateStrategy codeGenerateStrategy = codeGenerateStrategyFactory.getCodeGenerateStrategy(CodeGenerateType.MENU);
            String codePrefix = menu.getParent().equals("-1") ? "" : menu.getParent();
            menu.setCode(codeGenerateStrategy.generateCode(codePrefix, menu.getId().intValue()));
            menuMapper.updateById(menu);
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "新增失败，请稍后再试");
    }
    
    /**
     * 更新数据
     *
     * @param menu 实体
     * @return 更新结果
     */
    public ResponseData update(Menu menu) {
        // 更新的时候需要关注code的更新，这里默认都重新赋值
        CodeGenerateStrategy codeGenerateStrategy = codeGenerateStrategyFactory.getCodeGenerateStrategy(CodeGenerateType.MENU);
        String codePrefix = menu.getParent().equals("-1") ? "" : menu.getParent();
        menu.setCode(codeGenerateStrategy.generateCode(codePrefix, menu.getId().intValue()));
        //更新
        if (menuMapper.updateById(menu)>0){
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "修改失败，请稍后再试");
    }
    
    /**
     * 删除数据
     *
     * @param id 主键
     * @return 
     */
    public ResponseData delete(Long id) {
        if (menuMapper.deleteById(id)>0){
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }
    
    /**
     * 批量删除
     * @param idList
     * @return
     */
    public ResponseData deleteBatch(List<Long> idList){
        if (menuMapper.deleteBatchIds(idList)>0){
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 树形缩进选项列表查询
     * @param parent
     * @param deleted
     * @return
     */
    public ResponseData getIndentOptions(String parent, Integer deleted) {
        // 查询
        List<TreeOptions> treeOptions = menuMapper.selectTreeOptions(parent, deleted);
        // 整理成带缩进的列表
        SelectOptionsList optionsList = new SelectOptionsList();
        optionsList.makeIndentOptions(treeOptions);
        List<Map> menuOptions = optionsList.getIndentOptionsList();
        if (menuOptions == null || menuOptions.isEmpty()) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), menuOptions);
    }

    /**
     * 树形选项列表查询
     * @param parent
     * @param deleted
     * @return
     */
    public ResponseData getTreeOptions(String parent, Integer deleted) {
        // 查询
        List<TreeOptions> treeOptions = menuMapper.selectTreeOptions(parent, deleted);
        if (treeOptions == null || treeOptions.isEmpty()) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), treeOptions);
    }

    /**
     * 树形表格查询
     * @param menu
     * @return
     */
    public ResponseData getTreeTable(Menu menu) {
        // 重新构建查询参数
        QueryMenu queryMenu = new QueryMenu();
        queryMenu.init(menu);
        // 查询
        List<TreeMenuTable> treeMenuTables = menuMapper.selectTreeMenuTable(queryMenu);
        if (treeMenuTables == null || treeMenuTables.isEmpty()) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), treeMenuTables);
    }
}
